import scrapy

from ScrapyObject.spiders.utils.url_utils import *

'''
# 第一次启动（开始爬取）
# 再次运行，自动从断点继续
scrapy crawl ydaz -s JOBDIR=/home/ccg/crawls/mytask -o ydaz.json
DOWNLOAD_DELAY = 0.1
scrapy crawl ydaz -o ydaz.json
https://admit.zkqtusy.xyz/
'''


class YdazSpider(scrapy.Spider):
    name = "ydaz"
    # 前缀
    prefix = 'https://admit.'
    # 中缀
    website = 'zkqtusy'
    # 后缀
    suffix = '.xyz/'
    allowed_domains = [website + '.xyz']
    start_urls = [prefix + website + suffix]

    def __init__(self):
        self.i = 0

    def parse(self, response):
        content = get_data(response)
        picture_url = response.xpath("//div[@id='mse']/@ data-poster").extract()
        video_url = response.xpath("//div[@id='mse']/@ data-url").extract()
        tags = response.xpath("//meta[@property='video:tag']/@ content").extract()
        names = response.xpath("//h1[@class='dx-title text-white leading-22']/text()").extract()
        if len(picture_url) and len(video_url) and len(tags) and len(names):
            self.i = self.i + 1
            yield get_video_item(id=self.i, tags=tags[0], url="", name=names[0], pUrl=picture_url[0], vUrl=get_video_url_one(video_url[0])[0])
        url_list = get_url(content)
        for url in url_list:
            if url.startswith('/'):
                new_url = split_joint(self.prefix + self.website + self.suffix, url)
                if not (new_url.startswith(split_joint(self.prefix + self.website + self.suffix, '/video/index/postNovel'))
                        or new_url.startswith(split_joint(self.prefix + self.website + self.suffix, '/video/index/novel'))
                        or new_url.startswith(split_joint(self.prefix + self.website + self.suffix, '/video/index/links?key=question&v='))
                        or new_url.startswith(split_joint(self.prefix + self.website + self.suffix, '/video/index/links?key=home&v='))
                        or new_url.startswith(split_joint(self.prefix + self.website + self.suffix, '/video/index/links?key=privacy&v='))
                        or new_url.startswith(split_joint(self.prefix + self.website + self.suffix, '/video/index/links?key=protocol&v='))
                        or new_url.startswith(split_joint(self.prefix + self.website + self.suffix, '/video/index/links?key=dmca&v='))
                        or new_url.startswith(split_joint(self.prefix + self.website + self.suffix, '/video/index/links?key=2257&v='))
                        or new_url.startswith(split_joint(self.prefix + self.website + self.suffix, '/video/index/hotsearch?v='))):
                    if "?vkey=" in new_url and "&v=" in new_url:
                        result = new_url.split('&v=')[0]
                        if not result.startswith(split_joint(self.prefix + self.website + self.suffix, '?v=')):
                            yield scrapy.Request(result, callback=self.parse)
                    else:
                        if not new_url.startswith(split_joint(self.prefix + self.website + self.suffix, '?v=')):
                            yield scrapy.Request(new_url, callback=self.parse)
